<?php
require(dirname(__FILE__) . '/includes/init.php');
require_once('./includes/Response.php');
// dv申请生成申请记录
if($_REQUEST['act'] == 'dv_apply'){
    $user_id = isset($_REQUEST['user_id'])?trim($_REQUEST['user_id']):'';
    $time = date("Y-m-d H:i:s");
    if(!empty($user_id)){
        $sql = "INSERT INTO ".$ecs->table('dv')." (user_id,time,status) VALUES ('$user_id','$time',2)";
        $db->query($sql);
        $row = mysql_affected_rows();
        if($row > 0){
            Response::json(201,"插入成功");
        }else{
            Response::json(432,"插入失败");
        }
    }
}
//获取用户相关信息传递给后台
if($_REQUEST['act' == 'to_worker']){
    $user_id = isset($_REQUEST['user_id'])?trim($_REQUEST['user_id']):'';
    if(!empty($user_id)){
        $sql = "SELECT * FROM ".$ecs->table('dv')." WHERE user_id = '$user_id' ORDER BY time";
        $row6 = $db->getAll($sql);
        if(!empty($row)){
            $sql = "SELECT * FROM ".$ecs->table('user')." WHERE user_id = '$user_id'";
            $row7 = $db->getRow($sql);
            if(!empty($row7)) {
                if (!empty($_POST['user_id'])) {
                    $sql = "SELECT user_name,user_sex,user_pic" .
                        " FROM " . $ecs->table('user') .
                        " WHERE  user_id = '" . $_POST['user_id'] . "' ";
                    $row1 = $db->getRow($sql);//查询基本信息顶端的用户名，头像，性别
                    $sql = "SELECT activity_id,activity_date,share_time" .
                        " FROM " . $ecs->table('share_activity') .
                        " WHERE  user_id = '" . $_POST['user_id'] . "' AND status=1 " .
                        "ORDER BY activity_date,share_time" . " LIMIT $page_start,$page_size ";
                    $row6 = $db->getAll($sql);//查询活动id,发布时间,活动时间


                    foreach ($row6 as $key => $value) {
                        $sql = "SELECT activity_name,activity_cover,activity_re_time,activity_date,activity_address,fab_num,activity_describe" .
                            " FROM " . $ecs->table('activity') .
                            " WHERE  activity_id = '" . $value['activity_id'] . "' ";//查询活动名称，封面，发布时间，地址，点赞数，活动描述
                        $row2 = $db->getRow($sql);
                        $sql = "SELECT user_name,user_sex,user_pic" .
                            " FROM " . $ecs->table('user') .
                            " WHERE  user_id = '" . $row2['user_id'] . "' ";
                        $row5 = $db->getRow($sql);       //查询发布者信息 ;

                        $sql = "SELECT ecs_user.user_name,ecs_user.user_pic,ecs_user.user_id FROM " . $ecs->table('user_activity') . "LEFT JOIN " . $ecs->table('user') . "ON ecs_user.user_id = ecs_user_activity.user_id" . " WHERE ecs_user_activity.activity_id ='" . $value['activity_id'] . "'";
                        $row3 = $db->getAll($sql);//查询活动参与者用户名，头像，ID

//        $sql = "select count(*) from".$ecs->table('like')."LEFT JOIN ".$ecs->table('user')."ON ecs_like.user_id=ecs_user.user_id"." WHERE ecs_like.activity_id ='" .$_POST['activity_id']."'";
                        $row4 = count($row3);//计算参与者数量

                        $now_time = strtotime(date("Y-m-d H:i:s"));//当前时间
                        $re_time = strtotime($row6['share_time']);//活动发布时间
                        $time = $now_time - $re_time;//计算时间差

                        $row[] = array(
                            'top_information' => $row1,
                            'publisher' => $row5,
                            'middle_information' => $row2,
                            'count_visit' => $row4,
                            'time_difference' => $time,
                            'member' => $row3,
                            'user_info' => $row7,
                            'dv_info' => $row7
                        );
                    }
                    return Response::json(200,"用户信息获取成功",$row);
                }
            }else{
                return Response::json(413,"用户信息获取失败");
            }
        }else{
            return Response::json(412,"DV 列表获取失败");
        }
    }else{
        return Response::json(411,"用户id获取失败");
    }
}
//后台判断后对用户申请进行修改
//只能为0,1   0=》申请失败 1=》申请成功   2=》申请中
if($_REQUEST['act'] == 'worker_change'){
    $id = isset($_REQUEST['id'])?trim($_REQUEST['id']):'';   //dv id
    $status = isset($_REQUEST['status'])?trim($_REQUEST['status']):'';  //申请状态
    $arr = array(0,1);

    if(empty($user_id)){
        Response::json(411,"用户id获取失败");
    }
    if(empty($status)){
        Response::json(412,"用户状态获取失败");
    }elseif (!in_array($status,$arr)){
        Response::json(413,"数据不合法");
    }

    $sql = "UPDATE ".$ecs->table('dv')." SET status = '$status' WHERE id = '$id'";
    $db->query($sql);
    $row = mysql_affected_rows();
    if($row > 0){
        Response::json(201,"修改成功");

    }else{
        Response::json(401,"修改失败");
    }
}